﻿using MongoDB.Driver;
using Publicuse.DBCore.Mongodb;
using Publicuse.Interface;
using Publicuse.Entity;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Service
{
    /// <summary>
    /// 统一业务服务
    /// </summary>
    public class MongodbService : IMongodbService
    {
        private int dbIns = -1;

        public MongodbService()
        {
            this.dbIns = AppConfigUtil.ReadAppSettings("dbMongodbType").GetIntValue();
        }

        public bool Add<T>(string tbname, T t, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).Add<T>(tbname, t, out msg);
        }

        public Task<AsyncModel<T>> AddAsync<T>(string tbname, T t)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).AddAsync<T>(tbname, t);
        }

        public bool ClearTable<T>(string oldname, string newname, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).ClearTable<T>(oldname, newname, out msg);
        }

        public long Count<T>(string tbname, FilterDefinition<T> filter, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).Count<T>(tbname, filter, out msg);
        }

        public Task<AsyncModel<T>> CountAsync<T>(string tbname, FilterDefinition<T> filter)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).CountAsync<T>(tbname, filter);
        }

        public bool DeleteMany<T>(string tbname, FilterDefinition<T> filter, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).DeleteMany<T>(tbname, filter, out msg);
        }

        public Task<AsyncModel<T>> DeleteManyAsync<T>(string tbname, FilterDefinition<T> filter)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).DeleteManyAsync<T>(tbname, filter);
        }

        public bool DeleteOne<T>(string tbname, string id, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).DeleteOne<T>(tbname, id, out msg);
        }

        public Task<AsyncModel<T>> DeleteOneAsync<T>(string tbname, string id)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).DeleteOneAsync<T>(tbname, id);
        }

        public bool DeleteTable(string tbname, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).DeleteTable(tbname, out msg);
        }

        public bool FindOne<T>(string tbname, string id, out string msg, string[] field = null)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).FindOne<T>(tbname, id, out msg, field);
        }

        public Task<AsyncModel<T>> FindOneAsync<T>(string tbname, string id, string[] field = null)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).FindOneAsync<T>(tbname, id, field);
        }

        public bool GetList<T>(string tbname, out string msg, FilterDefinition<T> filter, out List<T> list, string[] field = null, SortDefinition<T> sort = null)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).GetList(tbname, out msg, filter, out list, field, sort);
        }

        public Task<AsyncModel<T>> GetListAsync<T>(string tbname, FilterDefinition<T> filter, string[] field = null, SortDefinition<T> sort = null)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).GetListAsync<T>(tbname, filter, field, sort);
        }

        public bool GetListByPage<T>(string tbname, FilterDefinition<T> filter, int pageIndex, int pageSize, out long count, out List<T> list, out string msg, string[] field = null, SortDefinition<T> sort = null)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).GetListByPage<T>(tbname, filter, pageIndex, pageSize, out count, out list, out msg, field, sort);
        }

        public Task<AsyncModel<T>> GetListByPageAsync<T>(string tbname, int pageIndex, int pageSize, FilterDefinition<T> filter, SortDefinition<T> sort = null, string[] field = null)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).GetListByPageAsync<T>(tbname, pageIndex, pageSize, filter, sort, field);
        }

        public void GetTableInstance<T>(string tbname, Action<IMongoCollection<T>> action, out string msg) where T : class, new()
        {
            DBHelperUtil<MongodbInstance>.GetInstance(dbIns).GetTableInstance<T>(tbname, action, out msg);
        }

        public bool InsertMany<T>(string tbname, List<T> t, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).InsertMany<T>(tbname, t, out msg);
        }

        public Task<AsyncModel<T>> InsertManyAsync<T>(string tbname, List<T> t)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).InsertManyAsync<T>(tbname, t);
        }

        public bool Update<T>(string tbname, T t, string id, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).Update<T>(tbname, t, id, out msg);
        }

        public Task<AsyncModel<T>> UpdateAsync<T>(string tbname, T t, string id)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).UpdateAsync<T>(tbname, t, id);
        }

        public bool UpdateManay<T>(string tbname, Dictionary<string, string> dic, FilterDefinition<T> filter, out string msg)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).UpdateManay<T>(tbname, dic, filter, out msg);
        }

        public Task<AsyncModel<T>> UpdateManayAsync<T>(string tbname, Dictionary<string, string> dic, FilterDefinition<T> filter)
        {
            return DBHelperUtil<MongodbInstance>.GetInstance(dbIns).UpdateManayAsync<T>(tbname, dic, filter);
        }
    }
}
